<?php
	
	class LoginView {
		
		private $login = "login";
		private $logout = "logout";
		private $passWord = "passWord";
		private $regButton = "regButton";
		private $rememberMe = "rememberMe";
		private $userName = "userName";
		
		//Funktioner för att skriva ut "formuläret".
		public function DoLoginBox() {
			return "
			<form method='post' id='login'>
				<div id='box'>
					<label>Användarnamn:</label>
					<input type='text' name='$this->userName' />
				</div>
				<div id='box'>
					<label>Lösenord:</label>
					<input type='password' name='$this->passWord' />
				</div>
				<div id='boxC'>
					<div id='checkBox'>
						<input type='checkbox' name='$this->rememberMe' value='rememberMe' class='checkBox' id='checkBox' />
						<label>Kom ihåg mig</label>
					</div>
				</div>
				<input type='submit' value='Logga in' name='$this->login' class='button'>
			</form>";
		}
		
		public function DoLogoutBox() {
			
			return "
				<form method='post' id='formLogout'>
					<button name='$this->logout' class='button'>Logga ut</button>
				</form>";
		}
		
		public function CantLogin() {
			return "
				<div id='wrongDiv'>
					Fel användarnamn eller lösenord.
				</div>";
		}
		
		//Funktioner som returnerar användarnamnet och lösenordet.
		public function GetUserName() {		
			if (isset($_POST[$this->userName]) == true) {
				return $_POST[$this->userName];
			}	
			else if (isset($_COOKIE[$this->userName])) {
				return $_COOKIE[$this->userName] = true;
			}	
			return false;
		}
		
		public function GetPassword() {
			if (isset($_POST[$this->passWord]) == true) {
				return $_POST[$this->passWord];
			}		
			else if (isset($_COOKIE[$this->passWord])) {
				return $_COOKIE[$this->passWord] = true;
			}	
			return false;
		}
			
		//Funktion som sätter kakorna.
		public function setCookies() {	
			setcookie($this->userName, $this->GetUserName(), time() + 60*60*24*365);
			$_COOKIE[$this->userName] = true;
			
			setcookie($this->passWord, md5($this->GetPassword()), time() + 60*60*24*365);
			$_COOKIE[$this->passWord] = true;
		}
		
		public function unsetCookies() {
			setcookie($this->userName, $this->GetUserName(), time() - 3600);		
			unset($_COOKIE[$this->userName]);
			
			setcookie($this->passWord, $this->GetPassword(), time() - 3600);
			unset($_COOKIE[$this->passWord]);
		}
		
		//Kollar om knapparna login eller logout har tryckts/remember me icheckad.
		public function TriedToLogin() {
			if (isset($_POST[$this->login]) == true) {
				return true;
			} else if (isset($_COOKIE[$this->userName])) {
				return true;
			}
			return false;
		}
		
		public function TriedToLogOut() {
			if (isset($_POST[$this->logout]) == true) {
				return true;
			}		
			return false;
		}
		
		public function RememberMe() {
			if (isset($_POST[$this->rememberMe]) == true) {
				return true;
			}
			return false;
		}
		
		public function RegButton() {
			if (isset($_POST[$this->regButton]) == true) {
				return true;
			}		
			return false;
		}
	}
